package com.xxl.job.executor.service.impl;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.common.util.DBUtils;
import com.xxl.job.executor.common.util.SpringContextHolder;
import com.xxl.job.executor.service.IGrantryHourService;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;

@Service("grantryHourService")
public class GrantryHourService implements IGrantryHourService {
    Connection connPre = null;

    @Override
    public void transfer() throws Exception {
        XxlJobLogger.log("=====开始执行业务操作===");
        final String recordName = "contrast_Grantry_hour";
        try {
            DataSource dataSourcePre = SpringContextHolder.getBean("dataSourcePre");
            connPre = DataSourceUtils.getConnection(dataSourcePre);
            connPre.setAutoCommit(false);

            String recordStr = DBUtils.getCurSynRecordNum(connPre, recordName);
            XxlJobLogger.log("[普通]本次最大记录RECORDID为:" + recordStr);

            String dbCurTime = DBUtils.getDBCurTime(connPre);
            XxlJobLogger.log("[普通]本次同步截止时间为:" + dbCurTime);

            String sql = "insert into TB_SYNC_GBUPLOAD_ETCSU(recordid,collectid,gantryid,computerorder,collectdate,collecthourbatch," +
                    "etctypecount,etcclasscount,etcsuccesscount,etcsuccessfee,etcfailcount,etcfailfee,cpctypecount,cpcclasscount," +
                    "cpcsuccesscount,cpcsuccessfee,cpcfailcount,cpcfailfee,detectioncount,vehicledatacount,vehiclepiccount,transfertag," +
                    "receivetime,packname,batchcount,inserttime,updatetime) " +
                    "select SEQ_TB_SYNC_GBUPLOAD_ETCSU.nextval recordid,collectid,gantryid,computerorder,collectdate,collecthourbatch," +
                    "etctypecount,etcclasscount,etcsuccesscount,etcsuccessfee,etcfailcount,etcfailfee,cpctypecount,cpcclasscount," +
                    "cpcsuccesscount,cpcsuccessfee,cpcfailcount,cpcfailfee,detectioncount,vehicledatacount,vehiclepiccount,transfertag," +
                    "receivetime,packname,batchcount,sysdate inserttime,sysdate updatetime " +
                    "from contrast_Grantry_hour " +
                    "where inserttabletime>=to_date('" + recordStr + "','yyyy-mm-dd hh24:mi:ss') and inserttabletime<to_date('" + dbCurTime + "','yyyy-mm-dd hh24:mi:ss') ";
            Integer count = DBUtils.execute(connPre, sql);
            XxlJobLogger.log("[普通]本次从汇聚系统共同步承载门架小时批次(条):" + count);

            sql = "insert into TB_SYNC_GBUPLOAD_ETCSU(recordid,COLLECTID,GANTRYID,COMPUTERORDER,COLLECTDATE,COLLECTHOURBATCH,BATCHCOUNT,DOMAIN1,inserttime,updatetime) " +
                    "select SEQ_TB_SYNC_GBUPLOAD_ETCSU.nextval,COLLECTID,GANTRYID,COMPUTERORDER,COLLECTDATE,COLLECTHOURBATCH,BATCHCOUNT,'GBUPLOAD_ETCSU',sysdate,sysdate " +
                    "from GBUPLOAD_ETCSU " +
                    "where inserttabletime>=to_date('" + recordStr + "','yyyy-mm-dd hh24:mi:ss') and inserttabletime<to_date('" + dbCurTime + "','yyyy-mm-dd hh24:mi:ss') ";
            count = DBUtils.execute(connPre, sql);
            XxlJobLogger.log("[普通]本次从汇聚系统共同步真实门架小时批次(条):" + count);

            count = DBUtils.modifySynRecordNum(connPre, recordName, recordStr, dbCurTime);
            if (count == null || count != 1) {
                XxlJobLogger.log("[异常][普通]更新记录时间异常");
                throw new Exception("[普通]更新记录时间异常");
            }
            XxlJobLogger.log("[普通]更新记录时间为:" + dbCurTime);

            connPre.commit();
            XxlJobLogger.log("=====完成执行业务操作===");
        }catch(Exception e){
            if (connPre != null) {
                try {
                    connPre.rollback();
                    XxlJobLogger.log("回滚成功.");
                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
            XxlJobLogger.log("抛出异常："+e.getMessage());
            throw e;
        }finally{
            if (connPre != null) {
                try {
                    connPre.setAutoCommit(true);
                    connPre.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
